#ifndef CANVAS_H
#define CANVAS_H

#include <QObject>
#include <QDeclarativeItem>
#include <QPainter>

class Canvas : public QDeclarativeItem
{
    Q_OBJECT
    Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
    Q_PROPERTY(int penWidth READ penWidth WRITE setPenWidth NOTIFY penWidthChanged)
    Q_PROPERTY(int penX READ penX WRITE setPenX NOTIFY penXChanged)
    Q_PROPERTY(int penY READ penY WRITE setPenY NOTIFY penYChanged)
    Q_PROPERTY(QUrl underneathImage READ underneathImage WRITE setUnderneathImage NOTIFY underneathImageChanged)
    Q_PROPERTY(QSize sourceSize READ sourceSize WRITE setSourceSize NOTIFY sourceSizeChanged)
    Q_PROPERTY(Qt::PenCapStyle penCapStyle READ penCapStyle WRITE setPenCapStyle NOTIFY penCapStyleChanged)
    Q_PROPERTY(Qt::PenJoinStyle penJoinStyle READ penJoinStyle WRITE setPenJoinStyle NOTIFY penJoinStyleChanged)
public:
    explicit Canvas(QDeclarativeItem *parent = 0);

    Q_INVOKABLE void undo();
    Q_INVOKABLE void save(QString filename);
    Q_INVOKABLE QUrl createEmpty(int width,int height);
    Q_INVOKABLE void clear();
    Q_INVOKABLE QString freeFilename();
protected:
    void paint(QPainter* painter, const QStyleOptionGraphicsItem* styleOption, QWidget* widget);

private:
    QColor color(){return m_color;}
    void setColor(QColor color){m_color = color;emit colorChanged(m_color);}

    int penWidth(){return m_penWidth;}
    void setPenWidth(int penWidth){m_penWidth = penWidth; emit penWidthChanged(m_penWidth);}

    int penX(){return m_penX;}
    int penY(){return m_penY;}
    void setPenX(int penX){m_penX=penX; emit penXChanged(m_penX);update();}
    void setPenY(int penY){m_penY=penY; emit penYChanged(m_penY);update();}
    QUrl underneathImage(){return m_underneathImageFilename;}
    void setUnderneathImage(QUrl filename){m_underneathImageFilename=filename; emit underneathImageChanged(m_underneathImageFilename);}

    QSize sourceSize(){return m_sourceSize;}
    void setSourceSize(QSize sourceSize){m_sourceSize=sourceSize;emit sourceSizeChanged(m_sourceSize);}

    Qt::PenCapStyle penCapStyle(){return m_capStyle;}
    void setPenCapStyle(Qt::PenCapStyle capStyle){m_capStyle=capStyle;emit penCapStyleChanged();}
    Qt::PenJoinStyle penJoinStyle(){return m_joinStyle;}
    void setPenJoinStyle(Qt::PenJoinStyle joinStyle){m_joinStyle=joinStyle;emit penJoinStyleChanged();}

signals:
    void colorChanged(QColor color);
    void penWidthChanged(int penWidth);
    void penXChanged(int penX);
    void penYChanged(int penY);
    void saveFinished(bool success);
    void underneathImageChanged(QUrl filename);
    void sourceSizeChanged(QSize sourceSize);
    void penJoinStyleChanged();
    void penCapStyleChanged();

public slots:
    
private:
    QColor  m_color;
    int     m_penWidth;
    int     m_penX;
    int     m_penY;
    QList< QPair<QPointF,QPen> > m_penPoints;
    QUrl m_underneathImageFilename;
    QSize   m_sourceSize;
    Qt::PenCapStyle m_capStyle;
    Qt::PenJoinStyle m_joinStyle;

};

QML_DECLARE_TYPE(Canvas)

#endif // Canvas_H
